<?php
/**
 * Created by LíJimpéy(：
 * User: jimpey88
 * Date: 2024/3/26
 * Email: < jimpey@qq.com >
 * Project: company-admin-tp6
 */

namespace app\common\validate;

use app\common\Services\JsonService;
use think\Validate;

class BaseValidate extends Validate
{
    use JsonService;
    public string $method = 'GET';

    public function post()
    {
        if (!$this->request->isPost()) {
            self::throw('post.');
        }
        $this->method = 'POST';
        return $this;
    }
    public function get()
    {
        if (!$this->request->isGet()) {
            self::throw('get.');
        }
        return $this;
    }

    public function goCheck($scene = null, array $validateData = []): array
    {
        //接收参数
        if ($this->method == 'GET') {
            $params = request()->get();
        } else {
            $params = request()->post();
        }
        //合并验证参数
        $params = array_merge($params, $validateData);

        //场景
        if ($scene) {
            $result = $this->scene($scene)->check($params);
        } else {
            $result = $this->check($params);
        }

        if (!$result) {
            $exception = is_array($this->error) ? implode(';', $this->error) : $this->error;
            self::throw($exception);
        }
        // 3.成功返回数据
        return $params;
    }
}